package com.pinyoumei.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.pinyoumei.base.TaotaoResult;
import com.pinyoumei.component.JedisSingleClient;
import com.pinyoumei.domain.Cart;
import com.pinyoumei.mapper.TbOrderItemMapper;
import com.pinyoumei.mapper.TbOrderMapper;
import com.pinyoumei.pojo.TbOrder;
import com.pinyoumei.pojo.TbOrderItem;
import com.pinyoumei.service.OrderService;
import com.pinyoumei.utils.IdWorker;
import com.pinyoumei.utils.JsonUtils;

import lombok.extern.slf4j.Slf4j;

//@RestController=@ResponseBody+@Controller
@Slf4j
@RestController 
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private JedisSingleClient jedisClient;
	
	@Autowired
	private TbOrderMapper orderMapper;
	
	@Autowired
	private TbOrderItemMapper orderItemMapper;
	
    /**
     * localhost:8890/order/update?orderId=154289514816926&payId=999999999&status=9
     */
	@Override
	public String update(@RequestParam("orderId")Long orderId, @RequestParam("payId")String payId, @RequestParam("status")String status) {
		log.info("#########orderId:{},payId:{},status:{}",orderId,payId,status+"#########");
		TbOrder order=new TbOrder();
		order.setOrderId(orderId);
		order.setPayId(payId);
		order.setStatus(status);
		order.setUpdateTime(new Date());
		order.setPaymentTime(new Date());
		int result=orderMapper.updateByPrimaryKey(order);
		if (result <= 0) {
			return JsonUtils.objectToJson(TaotaoResult.build(201, "订单更新失败"));
		}
		return JsonUtils.objectToJson(TaotaoResult.build(200, "订单更新成功"));
	}

	/**
	 * 根据购物车添加订单
	 */
	@Override
	public String add(@RequestBody TbOrder order) {
		log.info("#########order:{}",JsonUtils.objectToJson(order));
		//1. 从cookie(redis)中提取购物车列表
		String cartKey="CART:"+order.getUserId();
		String cartListStr = jedisClient.get(cartKey);
		if(null==cartListStr || "".equals(cartListStr))	{
			return "购物车为空";
		}		
		List<Cart> cartList=JsonUtils.jsonToList(cartListStr, Cart.class);
		IdWorker idWorker=new IdWorker();
		//2. 循环购物车列表,创建订单
		for(Cart cart:cartList){
			long orderId = idWorker.nextId();
			log.info("sellerId:"+cart.getSellerId());
			TbOrder tborder=new TbOrder();//新创建订单对象
			tborder.setOrderId(orderId);//订单ID
			tborder.setUserId(order.getUserId());//用户名
			tborder.setPaymentType(order.getPaymentType());//支付类型
			tborder.setStatus("1");//状态：未付款
			tborder.setCreateTime(new Date());//订单创建日期
			tborder.setUpdateTime(new Date());//订单更新日期
			tborder.setReceiverAreaName(order.getReceiverAreaName());//地址
			tborder.setReceiverMobile(order.getReceiverMobile());//手机号
			tborder.setReceiver(order.getReceiver());//收货人
            tborder.setSourceType(order.getSourceType());//订单来源
			tborder.setSellerId(cart.getSellerId());//商家ID				
			//循环购物车明细
			double money=0;
			for(TbOrderItem orderItem :cart.getOrderItemList()){		
				orderItem.setId(idWorker.nextId());
				orderItem.setOrderId(orderId);//订单ID	
				orderItem.setSellerId(cart.getSellerId());
				money+=orderItem.getTotalFee().doubleValue();//金额累加
				orderItemMapper.insert(orderItem);				
			}
			tborder.setPayment(new BigDecimal(money));			
			orderMapper.insert(tborder);	
		}
		//3. 清除cokie(redis)购物车
		jedisClient.del(cartKey);
		return JsonUtils.objectToJson(TaotaoResult.build(200, "订单创建成功"));
	}


	@Override
	public String getCartList4userId(@RequestParam("userId") String userId) {
		String cartKey="CART:"+userId;
		String cartListStr = jedisClient.get(cartKey);
		return cartListStr;
	}

}
